/**  
 * @Title:  patPractice0019.java   
 * @Package com.wkr.pat.basiclevel   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: WuKangrong     
 * @date:   2018年7月22日 上午8:14:20   
 * @version V
 * @JDKversion:1.8.0
 * @Copyright: 2018  
*/
package com.wkr.pat.basiclevel;
import java.util.*;
/**   
 * @ClassName:  patPractice0019   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: WuKangrong 
 * @date:   2018年7月22日 上午8:14:20     
 * @Copyright: 2018 
 */
public class patPractice0019 {

	/**
	 * list 集合存放Integer实例
	 * input 控制台输入实例
	 * in_Num 输入的数值
	 * array_NumSort 存放四个Integer实例数值的数组
	 * numBit_1 存放四位数第1位值的实例
	 * numBit_2 存放四位数第2位值的实例
	 * numBit_3 存放四位数第3位值的实例
	 * numBit_4 存放四位数第4位值的实例
	 * numIncreaseSort 存放非递减的四位数
	 * numDecreaseSort 存放非递增的四位数
	 */
	static List<Integer> list = new ArrayList<Integer>();
	static Scanner input = new Scanner(System.in);
	static int[] array_NumSort = new int[4];
	static Integer in_Num;
	static Integer numBit_1;
	static Integer numBit_2;
	static Integer numBit_3;
	static Integer numBit_4;
	static int numIncreaseSort = 0;
	static int numDecreaseSort = 0;
	/**   
	 * @Title: main   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param args      
	 * @return: void      
	 * @throws   
	 */
	public static void main(String[] args) {
		inputComtrol();
		System.out.print("over");
	}
	
	/**   
	 * @Title: inputComtrol   
	 * @Description: TODO(输入交互)   
	 * @param:       
	 * @return: void      
	 * @throws   
	 */
	static void inputComtrol() 
	{
		System.out.println("请输入任意四位数正整数：");
		in_Num = input.nextInt();
		//dataConversion(in_Num);
		digitalBlackHole();
	}
	
	/**   
	 * @Title: digitalBlackHole   
	 * @Description: TODO(四位数的数字黑洞)   
	 * @param:       
	 * @return: void      
	 * @throws   
	 */
	static void digitalBlackHole()
	{
		// num_Flag 存放非递减和非递增两四位数的差
		int num_Flag = in_Num;
		while(num_Flag != 6174) {
			dataConversion(num_Flag);
			num_Flag = numDecreaseSort - numIncreaseSort;
			System.out.println(numDecreaseSort + " - " + numIncreaseSort + " = " + num_Flag);
			list = new ArrayList<Integer>();
		}
	}
	
	/**   
	 * @Title: dataConversion   
	 * @Description: TODO(一个四位数值转四个Integer实例，并进行非递增或非递减操作)   
	 * @param: @param num      
	 * @return: void      
	 * @throws   
	 */
	static void dataConversion(Integer num)
	{
		char[] array_Num;
		array_Num = num.toString().toCharArray();//转字符串后再转字符数组
		numBit_1 = Character.getNumericValue(array_Num[0]);
		list.add(numBit_1);
		numBit_2 = Character.getNumericValue(array_Num[1]);
		list.add(numBit_2);
		numBit_3 = Character.getNumericValue(array_Num[2]);
		list.add(numBit_3);
		numBit_4 = Character.getNumericValue(array_Num[3]);
		list.add(numBit_4);
		if(numBit_1.equals(numBit_2) && numBit_1.equals(numBit_3) && numBit_1.equals(numBit_4))
		{
			increaseSort();
			decreaseSort();
			System.out.println(numDecreaseSort + " - " + numIncreaseSort + " = " + 0);
			System.out.print("over");
			System.exit(0);
		}else {
			increaseSort();
			decreaseSort();
		}
		
	}
	/**   
	 * @Title: increaseSort   
	 * @Description: TODO(将集合内的四个Integer实例的数值按非递减排列并转成一个四位数的非递减数值)   
	 * @param:       
	 * @return: void      
	 * @throws   
	 */
	static void increaseSort()
	{
		int array_Length =0;
		Collections.sort(list);
		Iterator<Integer> it = list.iterator();//泛型，就限定在Integer类
		while(it.hasNext()) {
			array_NumSort[array_Length] = Integer.valueOf(it.next().toString());
			array_Length++;
		}
		numIncreaseSort = array_NumSort[0] * 1000 + array_NumSort[1] * 100 + array_NumSort[2] * 10 + array_NumSort[3];
	}
	
	/**   
	 * @Title: decreaseSort   
	 * @Description: TODO(将集合内的四个Integer实例的数值按非递增排列并转成一个四位数的非递增数值)   
	 * @param:       
	 * @return: void      
	 * @throws   
	 */
	static void decreaseSort()
	{
		int array_Length =0;
		Collections.sort(list, new NumComparator());
		Iterator<Integer> it = list.iterator();//泛型，就限定在Integer类
		while(it.hasNext()) {
			array_NumSort[array_Length] = Integer.valueOf(it.next().toString());
			array_Length++;
		}
		numDecreaseSort = array_NumSort[0] * 1000 + array_NumSort[1] * 100 + array_NumSort[2] * 10 + array_NumSort[3];
	}
}
